//
//  verificationCenterViewController.m
//  阿帕
//
//  Created by mac on 2020/1/13.
//  Copyright © 2020 GCY. All rights reserved.
//

#import "verificationCenterViewController.h"

@interface verificationCenterViewController ()
{
    UIView   *_codeBgview;
    UIView   *_passwordBgview;
    NSString *_type;
}
@property(nonatomic, strong)UITextField *telTextField;
@property(nonatomic, strong)UITextField *codeTextField;
@property(nonatomic, strong)UITextField *passwordTextField;
@property(nonatomic, strong)UIButton *confrimButton;

@end

@implementation verificationCenterViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    _type = @"phone";
    
    self.navigationItem.title = @"验证中心";
    
    self.view.backgroundColor = BackgroundColor;
    
    [self layouView];
}

- (void)layouView {
    
    //验证码验证
    _codeBgview = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, 150)];
    _codeBgview.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_codeBgview];
    
    NSDictionary *branch = [UserDefaultUtil valueForKey:Login_Branch];
    
    _telTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, ScreenW-30, 50)];
    _telTextField.font = font(16);
    _telTextField.text = CJStringWithFormat(@"%@", branch[@"phone"]);
    _telTextField.textColor = Dark_TextColor;
    _telTextField.keyboardType = UIKeyboardTypeNumberPad;
    _telTextField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
    //    _telTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    //    _telTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写手机号" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    _telTextField.enabled = NO;
    [_codeBgview addSubview:_telTextField];
    
    
    _codeTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, _telTextField.cy_bottom, 150, 50)];
    _codeTextField.font = font(16);
    _codeTextField.text = @"";
    _codeTextField.textColor = Dark_TextColor;
    _codeTextField.keyboardType = UIKeyboardTypeNumberPad;
    _codeTextField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
    _codeTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _codeTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写验证码" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    [_codeBgview addSubview:_codeTextField];
    
    NSArray *btnTitles = @[@"获取验证码",@"切换验证方式"];
    
    for (int i=0; i<2; i++) {
        UIButton *switchBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-215, _telTextField.cy_bottom + 50*i, 200, 50)];
        [switchBtn setTitle:btnTitles[i] forState:UIControlStateNormal];
        [switchBtn setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
        [switchBtn.titleLabel setFont:font(14)];
        [switchBtn addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
        switchBtn.tag = 100+i;
        switchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        [_codeBgview addSubview:switchBtn];
    }
    
    for (int i=1; i<3; i++) {
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(15, 50*i, ScreenW-30, 1)];
        line.backgroundColor = BackgroundColor;
        [_codeBgview addSubview:line];
    }
    
    
    //密码验证
    _passwordBgview = [[UIView alloc] initWithFrame:CGRectMake(0, 10, ScreenW, 100)];
    _passwordBgview.backgroundColor = [UIColor whiteColor];
    _passwordBgview.hidden = YES;
    [self.view addSubview:_passwordBgview];
    
    _passwordTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, ScreenW-30, 50)];
    _passwordTextField.font = font(16);
    _passwordTextField.text = @"";
    _passwordTextField.textColor = Dark_TextColor;
    _passwordTextField.keyboardType = UIKeyboardTypeDefault;
    _passwordTextField.backgroundColor = [UIColor cy_colorWithHexString:@"#fff"];
    _passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请填写登录密码" attributes:@{NSForegroundColorAttributeName: Light_TextColor}];
    [_passwordBgview addSubview:_passwordTextField];
    
    UIView *hLine = [[UIView alloc] initWithFrame:CGRectMake(15, 50, ScreenW-30, 1)];
    hLine.backgroundColor = BackgroundColor;
    [_passwordBgview addSubview:hLine];
    
    UIButton *switchBtn = [[UIButton alloc]initWithFrame:CGRectMake(ScreenW-215, _passwordTextField.cy_bottom, 200, 50)];
    [switchBtn setTitle:@"切换验证方式" forState:UIControlStateNormal];
    [switchBtn setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
    [switchBtn.titleLabel setFont:font(14)];
    [switchBtn addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
    switchBtn.tag = 150;
    switchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [_passwordBgview addSubview:switchBtn];
    
    
    _confrimButton = [[UIButton alloc]initWithFrame:CGRectMake(15, _codeBgview.cy_bottom + 30, ScreenW-30, 45)];
    [_confrimButton setTitle:@"确定" forState:UIControlStateNormal];
    [_confrimButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [_confrimButton setBackgroundColor:Theme_ColorValue];
    [_confrimButton.titleLabel setFont:font(17)];
    [_confrimButton addTarget:self action:@selector(btnTypeClick:) forControlEvents:UIControlEventTouchUpInside];
    _confrimButton.tag = 200;
    cut(_confrimButton, BTN_BORDER_RADIUS);
    [self.view addSubview:_confrimButton];
}

- (void)btnTypeClick:(UIButton *)button {
    WEAKSELF
    if (button.tag == 100) {
        //获取验证码
        NSDictionary *params = @{@"partyGroupCode": [UserDefaultUtil valueForKey:BranchCode],
                                 @"phone":CJStringWithFormat(@"%@", _telTextField.text),
                                 @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
        };
        
        [HTTPManager postNetworkRequest:[NSString stringWithFormat:@"%@%@",[NSString stringWithFormat:@"%@/ntocc-tps-api",[ReAddressDynamic shared].baseUrl],SMS_SENDVERIFICATIONCODE] withParameter:params withVC:self netName:@"获取验证码" success:^(id response, NSString *msg) {
            
            [XSInfoView showInfo:msg onView:weakSelf.view];
            
            [self theCountdown];
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
        
    } else if (button.tag == 101) {
        //切换验证方式
        _codeBgview.hidden = YES;
        _passwordBgview.hidden = NO;
        _confrimButton.cy_y = _passwordBgview.cy_bottom + 30;
        _type = @"passwd";
        
    } else if (button.tag == 150) {
        //切换验证方式
        _codeBgview.hidden = NO;
        _passwordBgview.hidden = YES;
        _confrimButton.cy_y = _codeBgview.cy_bottom + 30;
        _type = @"phone";
        
    } else if (button.tag == 200) {
        //确定
        
        NSDictionary *parmas = @{};
        
        if ([_type isEqual:@"phone"]) {
            
            if ([DCSpeedy isBlankString:_codeTextField.text] == YES) {
                [XSInfoView showInfo:@"验证码为空" onView:weakSelf.view];
                return;
            }
            
            parmas = @{
                @"type": _type,
                @"verifyCode": _codeTextField.text,
                @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
            };
            
        } else {
            
            if ([DCSpeedy isBlankString:_passwordTextField.text] == YES) {
                [XSInfoView showInfo:@"密码为空" onView:weakSelf.view];
                return;
            }
            
            parmas = @{
                @"type": _type,
//                @"passwd": _passwordTextField.text,
                @"passwd": [DCSpeedy base64:_passwordTextField.text],
                @"token":[UserDefaultUtil valueForKey:USER_TOKEN],
            };
        }
        
        [HTTPManager deleteNetworkRequest:CJStringWithFormat(@"%@%@", [NSString stringWithFormat:@"%@/ntocc-basic-api",[ReAddressDynamic shared].baseUrl], PARTY_CONFIRMBYE) withParameter:parmas withVC:self netName:@"确认注销" success:^(id response, NSString *msg) {
            
            [XSInfoView showInfo:msg onView:weakSelf.view];
            
            [KNotificationCenter postNotificationName:LOGIN_LOGOUT object:@{}];
            
            [UserDefaultUtil saveValue:@"" forKey:USER_TOKEN];
            
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void){
                
                [self.navigationController popToRootViewControllerAnimated:YES];
                
            });
            
        } failure:^(NSInteger code, NSString *msg) {
            
        }];
    }
}

- (void)theCountdown{
    
    UIButton *button = [self.view viewWithTag:100];
    
    __block int timeout= 59; //倒计时时间
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    
    dispatch_source_set_event_handler(_timer, ^{
        
        if(timeout<=0){ //倒计时结束，关闭
            
            dispatch_source_cancel(_timer);
            
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [button setTitle:@"获取验证码" forState:UIControlStateNormal];
                [button setTitleColor:Theme_ColorValue forState:UIControlStateNormal];
                [button setBackgroundColor:[UIColor whiteColor]];
                button.userInteractionEnabled = YES;
            });
            
        }else{
            
            int seconds = timeout % 60;
            
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                //设置界面的按钮显示 根据自己需求设置
                
                [UIView beginAnimations:nil context:nil];
                
                [UIView setAnimationDuration:0];
                
                [button setTitle:[NSString stringWithFormat:@"%@S",strTime] forState:UIControlStateNormal];
                //                [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                [UIView commitAnimations];
                button.userInteractionEnabled = NO;
                
            });
            timeout--;
        }
    });
    dispatch_resume(_timer);
}

@end
